package com.zang.im.croom.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author Zhang Qiang
 * @Date 2019/7/25 17:57
 */
@Component("springBeanUtil")
public class SpringBeanUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Autowired
    @Override
    public void setApplicationContext(ApplicationContext applicationContext)  throws BeansException {
        System.out.println("配置ApplicationContext");
        SpringBeanUtil.applicationContext = applicationContext;
    }

    public static Object getBean(String bean){
        return  applicationContext.getBean(bean);
    }
    public static <T> T getBeanByType(Class<T> clazz){
        return applicationContext.getBean(clazz);
    }


}
